{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LongContextRAG 使用范例\n",
    "\n",
    "本 notebook 展示了如何使用 `LongContextRAG` 类来处理长文本上下文的检索增强生成（RAG）任务。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-08-27 13:29:19,024\tINFO worker.py:1586 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-08-27 13:29:19,029\tINFO worker.py:1762 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33m(raylet)\u001b[0m The autoscaler failed with the following error:\n",
      "Terminated with signal 15\n",
      "  File \"/Users/jayce.zhao/miniconda3/envs/python310/lib/python3.10/site-packages/ray/autoscaler/_private/monitor.py\", line 709, in <module>\n",
      "    monitor.run()\n",
      "  File \"/Users/jayce.zhao/miniconda3/envs/python310/lib/python3.10/site-packages/ray/autoscaler/_private/monitor.py\", line 584, in run\n",
      "    self._run()\n",
      "  File \"/Users/jayce.zhao/miniconda3/envs/python310/lib/python3.10/site-packages/ray/autoscaler/_private/monitor.py\", line 438, in _run\n",
      "    time.sleep(AUTOSCALER_UPDATE_INTERVAL_S)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import byzerllm\n",
    "from autocoder.common import AutoCoderArgs\n",
    "from autocoder.rag.long_context_rag import LongContextRAG\n",
    "\n",
    "# 初始化 ByzerLLM\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"deepseek_chat\")\n",
    "\n",
    "# 设置参数\n",
    "args = AutoCoderArgs()\n",
    "\n",
    "# 指定包含文档的目录路径\n",
    "# /Users/allwefantasy/projects/llm_friendly_packages/github.com/allwefantasy/byzer-sql\n",
    "docs_path = \"~/workspace/dbt/demo\"  # 请替换为实际的文档目录路径\n",
    "\n",
    "# 初始化 LongContextRAG\n",
    "long_context_rag = LongContextRAG(llm, args, docs_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 LongContextRAG 进行问答"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "问题: ads_weekly_report_1有多少个字段?\n",
      "回答:\n",
      "没有找到相关的文档来回答这个问题。\n",
      "\n",
      "相关文档:\n"
     ]
    }
   ],
   "source": [
    "# 定义一个问题\n",
    "question = \"ads_weekly_report_1有多少个字段?\"\n",
    "\n",
    "# 使用 LongContextRAG 进行问答\n",
    "response_generator, contexts = long_context_rag.stream_chat_oai(\n",
    "    conversations=[{\"role\": \"user\", \"content\": question}]\n",
    ")\n",
    "\n",
    "# 打印回答\n",
    "print(\"问题:\", question)\n",
    "print(\"回答:\")\n",
    "for chunk in response_generator:\n",
    "    print(chunk, end=\"\")\n",
    "print(\"\\n\")\n",
    "\n",
    "# 打印相关上下文\n",
    "print(\"相关文档:\")\n",
    "for context in contexts:\n",
    "    print(f\"- {context}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多轮对话示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义多轮对话\n",
    "conversations = [\n",
    "    {\"role\": \"user\", \"content\": \"什么是深度学习？\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"深度学习是机器学习的一个子领域，它使用多层神经网络来模拟人脑的学习过程。深度学习模型可以自动从大量数据中学习特征，无需人工特征工程。这种方法在图像识别、自然语言处理和语音识别等领域取得了巨大突破。\"},\n",
    "    {\"role\": \"user\", \"content\": \"深度学习和传统机器学习有什么区别？\"}\n",
    "]\n",
    "\n",
    "# 使用 LongContextRAG 进行多轮对话\n",
    "response_generator, contexts = long_context_rag.stream_chat_oai(conversations)\n",
    "\n",
    "# 打印回答\n",
    "print(\"问题: 深度学习和传统机器学习有什么区别？\")\n",
    "print(\"回答:\")\n",
    "for chunk in response_generator:\n",
    "    print(chunk, end=\"\")\n",
    "print(\"\\n\")\n",
    "\n",
    "# 打印相关上下文\n",
    "print(\"相关文档:\")\n",
    "for context in contexts:\n",
    "    print(f\"- {context}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 解释 LongContextRAG 的工作原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LongContextRAG 的工作原理如下：\n",
    "\n",
    "1. 文档加载：从指定目录加载所有的 Markdown 文档。\n",
    "\n",
    "2. 相关性检查：对于给定的问题，使用 LLM 并行检查每个文档的相关性。\n",
    "\n",
    "3. 答案生成：使用相关文档作为上下文，生成问题的答案。\n",
    "\n",
    "4. 流式输出：以流式方式返回生成的答案，同时返回相关文档的信息。\n",
    "\n",
    "这种方法允许处理大量文档，同时保持较低的延迟，因为它只在相关文档上进行深入处理。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
